package com.flyan.game.motafx.core.map;

import com.flyan.game.motafx.config.GameConfig;
import com.flyan.game.motafx.core.enums.DirEnum;

/**
 * 地图位置
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/1/31
 */
public record MapPosition(
        int row, // 行号，从第一行开始
        int col // 列号，从第一列开始
) {

    /**
     * @return 当前位置是否正确
     */
    public boolean isCorrect() {
        return row >= 1 && row <= GameConfig.MAP_GRID_COUNT && col > 0 && col <= GameConfig.MAP_GRID_COUNT;
    }

    /**
     * 是否和某位置相邻
     *
     * @param other 其他位置
     * @return 是否相邻
     */
    public boolean isAdjacent(MapPosition other) {
        if (other.row == row - 1 || other.row == row + 1) {
            return other.col == col;
        }

        if (other.col == col - 1 || other.col == col + 1) {
            return other.row == row;
        }

        return false;
    }

    /**
     *
     * @param other
     * @return
     */
    public DirEnum whereDir(MapPosition other) {
        if (other.col == col) {
            if (other.row == row - 1) {
                return DirEnum.UP;
            }
            if (other.row == row + 1) {
                return DirEnum.DOWN;
            }
        }

        if (other.row == row) {
            if (other.col == col - 1) {
                return DirEnum.LEFT;
            }
            if (other.col == col + 1) {
                return DirEnum.RIGHT;
            }
        }

        return null;
    }

}
